<template>
  <div class="profile-page">
    <div class="profile-form">
      <!-- 头像上传区域 -->
      <el-upload
        class="avatar-uploader"
        action=""
        :http-request="handleUpload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <img v-if="userInfo.avatar" :src="userInfo.avatar" class="user-icon">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        <div class="upload-tip-bubble">点击头像上传 ~</div>
      </el-upload>

      <!-- 表单信息 -->
      <el-form ref="userForm" :model="userInfo" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="userInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="userInfo.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userInfo.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveInfo">保存</el-button>
          <el-button @click="goToDashboard">返回主页</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getUserInfo, UserInfoUpdate,avatarUpload } from '@/api/userInfo'

export default {
  data() {
    return {
      baseUrl: 'https://ljx666666.oss-cn-beijing.aliyuncs.com/',
      userInfo: {
        username: '',
        name: '',
        phone: '',
        email: '',
        avatar: ''
      }
    };
  },
  methods: {
    getUserInfo() {
      getUserInfo().then(res => {
        if (res.code === 20000) {
          if (Array.isArray(res.data) && res.data.length > 0) {
            const user = res.data[0];
            this.userInfo = {
              username: user.username,
              name: user.name,
              phone: user.phone,
              email: user.email,
              avatar: this.baseUrl + user.avatarUrl
            };
          } else {
            this.$message.error('获取用户信息失败：数据格式不正确');
          }
        } else {
          this.$message.error('获取用户信息失败');
        }
      }).catch(err => {
        console.error('获取用户信息失败：', err);
        this.$message.error('网络错误，请重试');
      });
    },
    handleAvatarSuccess(res, file) {
      this.userInfo.avatar = URL.createObjectURL(file.raw);
    },
    handleUpload({ file }){
      const formData = new FormData();
      formData.append('file', file);

      avatarUpload(formData).then(ret => {
        if (ret.code === 20000) {
          this.userInfo.avatar = this.baseUrl + ret.data;
          this.$message({
            type: 'success',
            message: '上传成功！'
          });
        }
      }).catch(err => {
        console.log(err);
        this.$message({ type: 'error', message: '上传失败，请稍后重试！' });
      });
    },
    saveInfo() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          UserInfoUpdate(this.userInfo)
            .then(ret => {
              if (ret.code === 20000) {
                this.$message({
                  type: 'success',
                  message: '更新成功!',
                  duration: 1000
                });
              }
            }).catch(err => {
              console.log(err);
              this.$message({ type: 'error', message: '上传失败，请稍后重试！' });
            });
        } else {
          this.$message({
            type: 'error',
            message: '请检查是否输入正确!'
          });
          return false;
        }
      });
    },
    goToDashboard() {
      this.$router.push('/dashboard'); // 跳转到主页
    }
  },
  created() {
    this.getUserInfo();
  }
};
</script>

<style scoped>
.profile-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../../assets/1.png');
  background-size: cover;
  background-position: center;
}
.profile-form {
  background: rgba(255, 255, 255, 0.5);
  padding: 60px;
  width: 700px; /* 设置更大的宽度 */
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.avatar-uploader {
  text-align: center;
  margin-bottom: 40px;
}
.user-icon {
  width: 120px;
  height: 120px;
  border-radius: 50%;
}
.avatar-uploader-icon {
  font-size: 36px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  border: 1px dashed #ccc;
  border-radius: 50%;
}
.upload-tip-bubble{
  position: absolute;
  top: 32%;
  left: 958px;
  transform: translateY(-50%);
  background-color: #fff;
  color: #333;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  white-space: nowrap;

  &::before {
    content: '';
    position: absolute;
    top: 50%;
    left: -10px;
    transform: translateY(-50%) rotate(45deg);
    width: 10px;
    height: 10px;
    background-color: #fff;
    border-top: 1px solid #ddd;
    border-left: 1px solid #ddd;
  }
}
.el-form-item {
  margin-bottom: 30px;
}
</style>
